package org.example.entity.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.parser.ParserConfig;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Copyright: Copyright (c) 2022 Asiainfo
 *
 * @ClassName: org.example.entity.vo.LoginUser
 * @Description: 该类的功能描述
 * @version: v1.0.0
 * @author: 炫沙糖桔的鹏仔
 * @date: 2022/8/9 11:16
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2022/8/9      炫沙糖桔的鹏仔          v1.0.0               修改原因
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails{


//    用户的权限
    private List<String> authorities;
    private User user;

    public LoginUser(User user) {
        this.user = user;
    }

    public LoginUser(User user, List<String> authorities) {
        this.user = user;
        this.authorities = authorities;
    }

    @JSONField(serialize = false)
//    配置不序列化
    private List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    /**
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        注入权限信息
        if(grantedAuthorities.size()!= 0){
            return grantedAuthorities;
        }
        //把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中
        grantedAuthorities = authorities.stream().
                map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return grantedAuthorities;
    }

    /**
     * @return
     */
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    /**
     * @return
     */
    @Override
    public String getUsername() {
        return user.getUserName();
    }

    /**
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
